package com.base.controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.Rechargeratios;
import com.base.po.voip.RechargeratiosExample;
import com.base.po.voip.RechargeratiosExample.Criteria;
import com.base.po.voip.Users;
import com.base.service.LogsService;
import com.base.service.RechargeratiosService;
import com.base.service.UsersService;
import com.base.utils.IP;
import com.base.utils.Produce;

@Controller
@RequestMapping("/admin")
public class content_tianjiaozaixianchongzhi {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private RechargeratiosService rechargeratiosService;
	@Autowired
	private UsersService usersService;

	// 账户信息
	@RequestMapping("/content_tianjiazaixianchongzhi")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		if (adminUserId == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
		RechargeratiosExample rechargeratiosExample = new RechargeratiosExample();
		Criteria a = rechargeratiosExample.createCriteria();
		a.andAgentidEqualTo(adminUserId).andDatastatusEqualTo(true);
		System.out.println(adminUserId);
		List<Rechargeratios> rechargeratios = rechargeratiosService.selectByExample(rechargeratiosExample);
		request.setAttribute("rechargeratios", rechargeratios);
        
		modelAndView.setViewName("jsp/content_tianjiazaixianchongzhi");
		return modelAndView;
	}
	
	
	String SUCCESS = "success";

	// 添加在线充值信息
	@RequestMapping(value = "/insertRechargeratios", method = RequestMethod.POST)
	public void insertRechargeratios(HttpServletRequest request, HttpServletResponse response, 
			String agentid, Integer money, Integer alimoney, Integer grade, String pic,
			Boolean datastatus, Integer shopmoney, String memo, String probationperiod
			) throws IOException, ParseException {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null) {
			out.print("请刷新页面重试！");
			return;
		}
		
		if(shopmoney == null) {
			shopmoney = 0 ;
		}
		
		if(grade == null || grade.equals("undefinde")) {
			grade = 0 ;
		}
		
		Users user = usersService.selectByPrimaryKey(adminId);
		String id = Produce.getUUID(); 
		int resultCode = rechargeratiosService.insert(new Rechargeratios(id, adminId, money, alimoney, grade, 
				pic, true, shopmoney, memo, probationperiod));
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		if (resultCode == 1) {
			
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+user.getUsername()+"1条在线充值页面信息", usernamesys,
					true, IP.getIpAddr(request));
			out.print(SUCCESS);
		} else {
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+user.getUsername()+"在线充值信息失败", usernamesys, true,
					IP.getIpAddr(request));
			out.print("更改失败，请您重试");
		}
	}

}
